package com.szsky.sky.demo;

import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.HashMap;
import java.util.Map;

public class BCryptPassword_TYYH {


    public static PasswordEncoder tyyhPasswordEncoder() {
        String encodingId = "bcrypt";
        BCryptPasswordEncoder bcryt = new BCryptPasswordEncoder();
        Map<String, PasswordEncoder> encoders = new HashMap<>();
        encoders.put(encodingId, bcryt);

        encoders.put("bsp", new BspMd5PasswordEncoder());
        encoders.put("noop", NoOpPasswordEncoder.getInstance());

        DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder(encodingId, encoders);
        passwordEncoder.setDefaultPasswordEncoderForMatches((PasswordEncoder) bcryt);
        return (PasswordEncoder) passwordEncoder;
    }




    // 一个手动生成的、固定的盐
//    private static final String FIXED_SALT = "$2a$10$IhExffCLgapb8kaIYTObM."; // 只生成一次并硬编码
//    private static final String FIXED_SALT = "$2a$10$gv/cXAJ9kcPeMvQMRrX0Pu"; // 只生成一次并硬编码
    //

    /**
     * 明文密码：Rongji@123
     * 生成的密码：{bcrypt}$2a$10$IhExffCLgapb8kaIYTObM.XzaAofz8v32YXP6tgJCssgyas0tDtti
     * 规则，去除{bcrypt}，取0-29位作为加密盐，使加密盐进行加密
     */
    private static final String FIXED_SALT = "$2a$10$IhExffCLgapb8kaIYTObM."; // 只生成一次并硬编码

    /**
     * 破解的加密，密码。
     */
    public static void bcryptPwd() {
        String rawPassword = "Rongji@123";

        System.out.println("使用的固定盐: " + FIXED_SALT);
        System.out.println(FIXED_SALT.length());

        // 第一次加密
        String hash1 = BCrypt.hashpw(rawPassword, FIXED_SALT);
        System.out.println("第一次加密结果: " + hash1);

        // 第二次加密
        String hash2 = BCrypt.hashpw(rawPassword, FIXED_SALT);
        System.out.println("第二次加密结果: " + hash2);

        // 验证两次结果是否相同
        System.out.println("------------------------------------");
        System.out.println("两次加密结果是否相同? " + hash1.equals(hash2)); // 输出: true

        // 验证密码是否正确
        boolean isMatch = BCrypt.checkpw(rawPassword, hash1);
        System.out.println("密码验证是否通过? " + isMatch); // 输出: true
    }

    public static void main(String[] args) {
        System.out.println("$2a$10$IhExffCLgapb8kaIYTObM.".length());
        bcryptPwd();
        // 原始密码
        // {bcrypt}$2a$10$IhExffCLgapb8kaIYTObM.XzaAofz8v32YXP6tgJCssgyas0tDtti
        System.out.println("密码是否相同：" + "$2a$10$IhExffCLgapb8kaIYTObM.XzaAofz8v32YXP6tgJCssgyas0tDtti".equals("$2a$10$IhExffCLgapb8kaIYTObM.XzaAofz8v32YXP6tgJCssgyas0tDtti"));
        System.out.println("$2a$10$IhExffCLgapb8kaIYTObM.XzaAofz8v32YXP6tgJCssgyas0tDtti".substring(0, 29));
        System.out.println("222222---"+"$2a$10$Tx1XP55jhirGjAvElJ8Jg.MkFhywiPkEmsyB8FLozXhn/GpjNaDE.".substring(0, 29));

        // 无密码方式
        System.out.println("无密码方式： {noop}123456");
    }
}
